/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddAllowedLayerForm.java
 *
 * Created on May 25, 2011, 10:30:13 AM
 */
package tasks.ui.architecture;

import common.ControllerException;
import common.DomainException;
import common.Observer;
import common.ObserverInterest;
import domain.DomainFacade;
import domain.DomainFacadeImpl;
import domain.LayerDTO;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author David
 */
public class AddAllowedLayerForm extends javax.swing.JPanel implements Observer {

    /** Creates new form AddAllowedLayerForm */
    public AddAllowedLayerForm() {
        initComponents();
        init();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addLayerLabel = new javax.swing.JLabel();
        addAllowedLayerBox = new javax.swing.JComboBox();
        addLayerButton = new javax.swing.JButton();

        addLayerLabel.setText("Layer:");

        addAllowedLayerBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        addLayerButton.setText("Add layer");
        addLayerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addLayerButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(addLayerLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(addAllowedLayerBox, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(addLayerButton))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(addLayerLabel)
                .addComponent(addLayerButton)
                .addComponent(addAllowedLayerBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void addLayerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addLayerButtonActionPerformed
        try {
            this.controller.addAllowedLayerToLayer((String) this.addAllowedLayerBox.getSelectedItem());
        } catch (DomainException ex) {
            Logger.getLogger(AddAllowedLayerForm.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ControllerException ex) {
            Logger.getLogger(AddAllowedLayerForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_addLayerButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox addAllowedLayerBox;
    private javax.swing.JButton addLayerButton;
    private javax.swing.JLabel addLayerLabel;
    // End of variables declaration//GEN-END:variables
    private ArchitectureController controller;
    private DomainFacade facade;

    @Override
    public void notifyChange(ObserverInterest interest) {
        this.setAllowedLayerBox();
    }

    public void setAllowedLayerBox() {
        if (this.controller != null) {
            try {
                this.addAllowedLayerBox.removeAllItems();

                Map<String, LayerDTO> availableAllowedLayers = this.controller.getAvailableAllowedLayers();

                Iterator<LayerDTO> i = availableAllowedLayers.values().iterator();
                while (i.hasNext()) {
                    this.addAllowedLayerBox.addItem(i.next().getName());
                }

                if (this.addAllowedLayerBox.getItemCount() > 0) {
                    this.addAllowedLayerBox.setEnabled(true);
                    this.addLayerButton.setEnabled(true);
                } else {
                    this.addAllowedLayerBox.setEnabled(false);
                    this.addLayerButton.setEnabled(false);
                }
            } catch (ControllerException ex) {
                this.addAllowedLayerBox.setEnabled(false);
            }
        }
    }

    private void init() {
        this.facade = DomainFacadeImpl.getInstance();
        this.facade.addObserver(this, ObserverInterest.LAYERS_CHANGED);
        this.addAllowedLayerBox.setEnabled(false);
        this.addLayerButton.setEnabled(false);
    }

    public void setController(ArchitectureController controller) {
        this.controller = controller;
        this.controller.addObserver(this, ObserverInterest.LAYER_SELECTION_CHANGED);
    }
}
